package com.xtoneict.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xtoneict.auth.entity.WorkTimeFile;
import com.xtoneict.auth.entity.YearMonthTab;
import com.xtoneict.auth.entity.param.FilePageParam;
import com.xtoneict.auth.entity.param.FilePageResultParam;
import com.xtoneict.auth.mapper.WorkTimeFileMapper;
import com.xtoneict.auth.mapper.YearMonthTabMapper;
import com.xtoneict.auth.service.IWorkTimeFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkTimeFileServiceImpl extends ServiceImpl<WorkTimeFileMapper, WorkTimeFile> implements IWorkTimeFileService {
    @Autowired
    private WorkTimeFileMapper workTimeFileMapper;
    @Autowired
    private YearMonthTabMapper yearMonthTabMapper;

    public Integer Insert(WorkTimeFile workTimeFile){
        return workTimeFileMapper.insert(workTimeFile);
    }

    public Integer update(WorkTimeFile workTimeFile){
        return workTimeFileMapper.updateById(workTimeFile);
    }


    //批量插入关系
    public Integer InsertUserAndFileBatch(List<Integer> userIds, List<Integer> fileIds){
        for (Integer userId : userIds){
            for (Integer fileId : fileIds){
                workTimeFileMapper.insertUserAndFile(userId, fileId);
            }
        }

        return 1;
    }

    //删除关系
    public Integer deleteUserAndFile(List<Integer> userIds, List<Integer> fileIds){
        for (Integer userId : userIds){
            for (Integer fileId : fileIds){
                workTimeFileMapper.deleteUserAndFile(userId, fileId);
            }
        }


        return 1;
    }

    //根据条件分页查询file
    public FilePageResultParam selectFiles(FilePageParam filePageParam){
        IPage<WorkTimeFile> page = new Page<>(filePageParam.getCurrentPage(),filePageParam.getPageSize());

        QueryWrapper<WorkTimeFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("pass_status");
        if(filePageParam.getFileName() != null){
            queryWrapper.like("file_name",filePageParam.getFileName());
        }
        if(filePageParam.getDepId() != null){
            queryWrapper.eq("dep_id",filePageParam.getDepId());
        }
        if (filePageParam.getEmployee() != null){
            queryWrapper.eq("employee_id",filePageParam.getEmployee());
        }
        if (filePageParam.getYear() != null && filePageParam.getMonth() != null){
            YearMonthTab yearMonthTab = yearMonthTabMapper.getByYearAndMonth(filePageParam.getYear(), filePageParam.getMonth());
            queryWrapper.eq("year_month_id", yearMonthTab.getYearMonthId());
        }

        Integer total = workTimeFileMapper.selectCount(queryWrapper);

        IPage<WorkTimeFile> filePage = workTimeFileMapper.selectPage(page, queryWrapper);
        List<WorkTimeFile> fileList = filePage.getRecords();

        FilePageResultParam filePageResultParam = new FilePageResultParam();
        filePageResultParam.setWorkTimeFiles(fileList);
        filePageResultParam.setTotal(total);

        return filePageResultParam;
    }

    //根据条件查询审核能看到的所有file
    public List<WorkTimeFile> selectFilesForManager(FilePageParam filePageParam){
        QueryWrapper<WorkTimeFile> queryWrapper = new QueryWrapper<>();
        if(filePageParam.getFileName() != null){
            queryWrapper.like("file_name",filePageParam.getFileName());
        }
        if (filePageParam.getEmployee() != null){
            queryWrapper.eq("employee_id",filePageParam.getEmployee());
        }
        if (filePageParam.getYear() != null && filePageParam.getMonth() != null){
            YearMonthTab yearMonthTab = yearMonthTabMapper.getByYearAndMonth(filePageParam.getYear(), filePageParam.getMonth());
            queryWrapper.eq("year_month_id", yearMonthTab.getYearMonthId());
        }

        List<WorkTimeFile> files = workTimeFileMapper.selectList(queryWrapper);

        return files;
    }

    //根据审核id查询工时表
    public List<Integer> selectFileIdsByUser(Integer userId){
        return workTimeFileMapper.selectFileIdsByUser(userId);
    }

    //根据工时表查询用户
    public List<Integer> selectUserIdByFile(Integer fileId){
        return workTimeFileMapper.selectUserIdsByFile(fileId);
    }
}
